package com.example.tests;
import java.util.concurrent.TimeUnit;

import com.example.pages.CreatedMoviePage;
import com.example.pages.LoginPage;
import com.example.pages.MovieListPage;
import org.junit.*;
import static org.junit.Assert.*;
import static org.openqa.selenium.support.ui.ExpectedConditions.*;

import org.openqa.selenium.remote.DesiredCapabilities;


import org.openqa.selenium.*;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.WebDriverWait;
import ru.esteru.selenium.factory.WebDriverFactory;
/**
 * Created with IntelliJ IDEA.
 * User: kzv
 * Date: 28.02.13
 * Time: 19:22
 * To change this template use File | Settings | File Templates.
 */
public class MovieClick {
    private WebDriver driver;
    private WebDriverWait wait;
    private String baseUrl;
    private boolean acceptNextAlert = true;
    private StringBuffer verificationErrors = new StringBuffer();
    private LoginPage loginPage;
    private MovieListPage newMovieListPage;
    private CreatedMoviePage newMoviecreatedPage;

    @Before
    public void setUp() throws Exception {
        int browser = 1;

        switch (browser){
            case 1: driver = WebDriverFactory.getDriver(DesiredCapabilities.firefox());
                break;
            case 2: driver = WebDriverFactory.getDriver(DesiredCapabilities.opera());
                break;
            case 3: driver = WebDriverFactory.getDriver(DesiredCapabilities.chrome());;
                break;
            default: driver = WebDriverFactory.getDriver(DesiredCapabilities.internetExplorer());;
        }
        baseUrl = "http://127.0.0.1/";
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        wait = new WebDriverWait(driver,10);
    }

    @Test
    public void testMovieClick() throws Exception {
        goToMainPage();
        loginAS("admin", "admin");
        newMovieListPage.movieOpen("Random"); //Movie name is "Random"
        assertTrue("This is not movie page", newMoviecreatedPage.isOnMoviePage());
        LogOut();
    }
    private void LogOut() {
        newMovieListPage.menuLogoutLink.click();
        wait.until(alertIsPresent()).accept();
    }

    private void loginAS(String username, String password) {
        loginPage.userNameField.clear();
        loginPage.userNameField.sendKeys(username);
        loginPage.passwordField.clear();
        loginPage.passwordField.sendKeys(password);
        loginPage.loginButton.click();

    }

    private void goToMainPage() {
        // open main page
        driver.get(baseUrl + "/php4dvd/");
        loginPage = PageFactory.initElements(driver, LoginPage.class);
        newMovieListPage = PageFactory.initElements(driver, MovieListPage.class);
        newMoviecreatedPage = PageFactory.initElements(driver, CreatedMoviePage.class);
    }

    @After
    public void tearDown() throws Exception {
        //driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }


}
